package com.ruoyi.web.controller.self;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.self.domain.RmCourserecord;
import com.ruoyi.self.service.IRmCourserecordService;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.IRmUserDeptService;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 课程学习记录Controller
 * 
 * @author ruoyi
 * @date 2019-12-21
 */
@Controller
@RequestMapping("/self/courserecord")
public class RmCourserecordController extends BaseController
{
    private String prefix = "self/courserecord";

    @Autowired
    private IRmCourserecordService rmCourserecordService;

    @Autowired
    private IRmUserDeptService rmUserDeptService;

    @Autowired
    private ISysDeptService deptService;

    @RequiresPermissions("self:courserecord:view")
    @GetMapping()
    public String courserecord(ModelMap mmap)
    {
        Subject subject = SecurityUtils.getSubject();
        SysUser sysUser = (SysUser) subject.getPrincipal();
        List<SysDept> sysDepts;
        if(subject.hasRole("instructor")){
            sysDepts = rmUserDeptService.selectDeptsSelectByUserId(sysUser.getUserId());
            mmap.put("userId",sysUser.getUserId());
        } else {
            SysDept sysDept = new SysDept();
            sysDept.setParentId((long) 110);
            sysDepts = deptService.selectDeptList(sysDept);
            mmap.put("userId","");
        }
        mmap.put("mageDepts",sysDepts);
        return prefix + "/courserecord";
    }

    /**
     * 查询课程学习记录列表
     */
    @RequiresPermissions("self:courserecord:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RmCourserecord rmCourserecord)
    {
        startPage();
        List<RmCourserecord> list = rmCourserecordService.selectRmCourserecordList(rmCourserecord);
        return getDataTable(list);
    }

    /**
     * 查询课程学习记录列表,学生端
     */
    @RequiresPermissions("self:courserecord:list")
    @PostMapping("/list2")
    @ResponseBody
    public List<Map<String,Object>> list2(RmCourserecord rmCourserecord)
    {
        List<Map<String,Object>> list = rmCourserecordService.selectRmCourserecordList2(rmCourserecord);
        return list;
    }


    /**
     * 导出课程学习记录列表
     */
    @RequiresPermissions("self:courserecord:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RmCourserecord rmCourserecord)
    {
        List<RmCourserecord> list = rmCourserecordService.selectRmCourserecordList(rmCourserecord);
        ExcelUtil<RmCourserecord> util = new ExcelUtil<RmCourserecord>(RmCourserecord.class);
        return util.exportExcel(list, "courserecord");
    }

    /**
     * 新增课程学习记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存课程学习记录
     */
    @RequiresPermissions("self:courserecord:add")
    @Log(title = "课程学习记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RmCourserecord rmCourserecord)
    {
        return toAjax(rmCourserecordService.insertRmCourserecord(rmCourserecord));
    }

    /**
     * 修改课程学习记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        RmCourserecord rmCourserecord = rmCourserecordService.selectRmCourserecordById(id);
        mmap.put("rmCourserecord", rmCourserecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程学习记录
     */
    @RequiresPermissions("self:courserecord:edit")
    @Log(title = "课程学习记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RmCourserecord rmCourserecord)
    {
        return toAjax(rmCourserecordService.updateRmCourserecord(rmCourserecord));
    }

    /**
     * 删除课程学习记录
     */
    @RequiresPermissions("self:courserecord:remove")
    @Log(title = "课程学习记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(rmCourserecordService.deleteRmCourserecordByIds(ids));
    }

    /**
     * 更新课程学习记录时间
     */
    @Log(title = "课程学习记录时间更新", businessType = BusinessType.UPDATE)
    @PostMapping( "/updateTime")
    @ResponseBody
    public AjaxResult updateTime(RmCourserecord rmCourserecord)
    {
        Long cid = rmCourserecord.getCourseId();
        if(cid.equals("") || cid == null ) {
            return toAjax(false);
        }
        Subject subject = SecurityUtils.getSubject();
        SysUser principal = (SysUser)subject.getPrincipal();
        rmCourserecord.setLoginName(principal.getLoginName());
        return toAjax(rmCourserecordService.updateCourserecordTime(rmCourserecord));
    }
}
